package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.Action;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: liangWeiQUQ
 * @Description: TODO
 * @DateTime: 2021/2/27 17:11
 **/
@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
     @DubboReference
    private SetmealService setmealService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;
     /**
      * @Author: liangWeiQUQ
      * @Date: 17:15 2021/2/27
      * @Parms
      * @ReturnType:
      * @Description: 套餐分页查询
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]data: {}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    /**
     * @Author: liangWeiQUQ
     * @Date: 22:08 2021/2/27
     * @Parms MultipartFile 进行接收文件
     * @ReturnType:
     * @Description: 上传图片
     */
    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        /*
                1.上传到七牛云
                    2.上传完毕后将图片的地址返回
         */
        //用户上传后原始文件的名称
        String originalFilename = file.getOriginalFilename();//原图片名称
        log.info("[图片上传-原始文件名称] {}", originalFilename);
        //自己构造不重复的名称
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
        log.info("[图片上传-改造后不重复文件名称] {}", fileName);
        try {
            //调用七牛云工具类完成上传
            qiniuUtils.upload2Qiniu(file.getInputStream(),fileName);
            //上传文件到redis中
            redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).add(fileName);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        //拿到上传的地址
        String imgUrl = qiniuUtils.getUrlPrefix() + fileName;
        log.info("[图片上传-上传完毕后url地址] {}", imgUrl);
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imgUrl);
    }
        /**
         * @Author: liangWeiQUQ
         * @Date: 22:48 2021/2/27
         * @Parms
         * @ReturnType:
         * @Description: 套餐管理-添加套餐
        */
    @PostMapping("add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        log.info("[套餐管理-添加套餐]checkgroupIds : {}, setmeal： {}", checkgroupIds, setmeal);
        //将前缀的域名替换为空，有长度的限制
        String fileName = setmeal.getImg().replace(qiniuUtils.getUrlPrefix(), "");
        setmeal.setImg(fileName);
        setmealService.add(checkgroupIds, setmeal);
        //新增后从redis中删除图片
        redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).remove(fileName);
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }
}
